package tq.com.tqteacher.adapt.newPublish;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQAtachModel;

public class TQWKListAdapter extends RecyclerView.Adapter {
    private ArrayList<TQAtachModel> mVideoSetsModels = new ArrayList<>();

    private Context mContext;
    private boolean canModify = true;
    public TQWKListAdapter(Context pContext){
        mContext = pContext;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_wk_item_layout, pViewGroup, false);

        return new WKListItem(lView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        TQAtachModel lSetsModel = mVideoSetsModels.get(pI);
        ((WKListItem)pViewHolder).bindData(lSetsModel, pI + 1);
    }

    @Override
    public int getItemCount() {
        return mVideoSetsModels.size();
    }
    public void replaceData(ArrayList<TQAtachModel> pVideoSetsModels){
        if(pVideoSetsModels == null) return;
        mVideoSetsModels.clear();
        mVideoSetsModels.addAll(pVideoSetsModels);
        notifyDataSetChanged();
    }

    public void setCanModify(boolean pCanRecapture){
        canModify = pCanRecapture;
    }

    class WKListItem extends RecyclerView.ViewHolder{
        private TQAtachModel mTQVideoSetsModel;
        private ImageView deletView;
        private ImageView playView;
        private ImageView captureView;
        private ImageView video;

        private TextView wkIndex;
        private TextView wkTitle;

        public WKListItem(View pView){
            super(pView);
            deletView = pView.findViewById(R.id.delete);
            playView = pView.findViewById(R.id.play);
            captureView = pView.findViewById(R.id.flag);
            video = pView.findViewById(R.id.video);

            wkIndex = pView.findViewById(R.id.wk_index);
            wkTitle = pView.findViewById(R.id.title);

            deletView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mIClickListener != null)mIClickListener.deleteAttach(mTQVideoSetsModel);
                }
            });

            playView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mIClickListener != null)mIClickListener.play(mTQVideoSetsModel);
                }
            });

            captureView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mIClickListener != null)mIClickListener.recapture(mTQVideoSetsModel);
                }
            });
        }

        public void bindData(TQAtachModel pTQVideoSetsModel, int index){
            mTQVideoSetsModel = pTQVideoSetsModel;

            if(pTQVideoSetsModel == null) return;

            if(mTQVideoSetsModel.hasFile()){
                captureView.setImageResource(R.drawable.btn_recaption);
            }else{
                captureView.setImageResource(R.drawable.bt_nocapture);
            }
            captureView.setVisibility(canModify ? View.VISIBLE : View.INVISIBLE);
            deletView.setVisibility(canModify ? View.VISIBLE : View.INVISIBLE);

            wkIndex.setText(String.format("微课%d", index));
            wkTitle.setText(mTQVideoSetsModel.getDesc());

            String url = TQPictureUpload.downloadPath(mTQVideoSetsModel.getCover());
            RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.default_pic)
                    .error(R.drawable.default_pic);
            Glide.with(mContext).load(url).apply(lOptions).into(video);
        }
    }

    public interface IClickListener{
        void play(TQAtachModel pModel);
        void deleteAttach(TQAtachModel pModel);
        void recapture(TQAtachModel pModel);
    }

    private IClickListener mIClickListener;
    public void setIClickListener(IClickListener pIClickListener){
        mIClickListener = pIClickListener;
    }
}
